package com.py.korona;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;
import com.py.korona.ui.activities.category.CategoryListFragment;
import com.py.korona.ui.activities.favorites.FavoritesListFragment;
import com.py.korona.ui.activities.plan.PlanListFragment;
import com.py.korona.ui.activities.room.RoomListFragment;
import com.py.korona.ui.activities.searchresult.SearchResultListFragment;
import com.py.korona.ui.activities.settings.SettingsActivity;
import com.py.korona.ui.listeners.CustomTabListener;

public class MainActivity extends FragmentActivity {
    private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item";

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        boolean result = false;
        switch (item.getItemId()) {
            case R.id.menu_settings: {
                Intent intent = new Intent(this, SettingsActivity.class);
                startActivity(intent);
                break;
            }
            default: {
                result = super.onOptionsItemSelected(item);
                break;
            }
        }
        return result;
    }

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        if (savedInstanceState.containsKey(STATE_SELECTED_NAVIGATION_ITEM)) {
            getActionBar().setSelectedNavigationItem(savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM));
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        outState.putInt(STATE_SELECTED_NAVIGATION_ITEM, getActionBar().getSelectedNavigationIndex());
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTheme(R.style.MyTheme);
        setContentView(R.layout.activity_main);
        // Set up the action bar to show tabs.
        final ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        /** room Tab */
        Tab planListTab = actionBar.newTab();
        planListTab.setText("plan");
        Fragment planListFragment = new PlanListFragment();
        CustomTabListener planListMainActivityTabListener = new CustomTabListener(planListFragment, R.id.container);
        planListTab.setTabListener(planListMainActivityTabListener);
        actionBar.addTab(planListTab);
        /** room Tab */
        Tab roomListTab = actionBar.newTab();
        roomListTab.setText("rooms");
        Fragment roomListFragment = new RoomListFragment();
        CustomTabListener roomListMainActivityTabListener = new CustomTabListener(roomListFragment, R.id.container);
        roomListTab.setTabListener(roomListMainActivityTabListener);
        actionBar.addTab(roomListTab);
        /** category Tab */
        Tab categoryListTab = actionBar.newTab();
        categoryListTab.setText("categories");
        Fragment categoryListFragment = new CategoryListFragment();
        CustomTabListener categoryListTabListener = new CustomTabListener(categoryListFragment, R.id.container);
        categoryListTab.setTabListener(categoryListTabListener);
        actionBar.addTab(categoryListTab);
        /** favorites Tab */
        Tab favoritesTab = actionBar.newTab();
        favoritesTab.setText("Favorites");
        Fragment favoritesListFragment = new FavoritesListFragment();
        CustomTabListener favoritesListMainActivityTabListener = new CustomTabListener(favoritesListFragment, R.id.container);
        favoritesTab.setTabListener(favoritesListMainActivityTabListener);
        actionBar.addTab(favoritesTab);
        /** search result Tab */
        Tab searchResultTab = actionBar.newTab();
        searchResultTab.setText("Search results");
        Fragment searchResultListFragment = new SearchResultListFragment();
        CustomTabListener searchResultListMainActivityTabListener = new CustomTabListener(searchResultListFragment, R.id.container);
        searchResultTab.setTabListener(searchResultListMainActivityTabListener);
        actionBar.addTab(searchResultTab);
        //        try {
        //            // FakeDataInitializer.appendRecords(getContentResolver());
        //        } catch (Exception e) {
        //            // TODO Auto-generated catch block
        //            e.printStackTrace();
        //        }
    }
}
